import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;


public class UserInterface {

	private JFrame frame;
	private JList listFolderPaths;
	ArrayList<String> folderPaths;
	FileIOService fileService;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserInterface window = new UserInterface();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UserInterface() {
		fileService = new FileIOService();
		getFolderPaths();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 800, 455);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnAdd = new JButton();
		
		JButton btnEditCaption = new JButton();
		
		JButton btnRemove = new JButton();
		
		JButton btnUploadFolders = new JButton();
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 622, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnEditCaption, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
						.addComponent(btnAdd, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
						.addComponent(btnRemove, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
						.addComponent(btnUploadFolders, GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnAdd)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnEditCaption)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnRemove)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnUploadFolders))
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE))
					.addContainerGap())
		);
		
		listFolderPaths = new JList();
		scrollPane.setViewportView(listFolderPaths);
		frame.getContentPane().setLayout(groupLayout);
		
		btnAdd.setAction(new AddFolderAction(this));
		btnEditCaption.setAction(new EditCaptionAction(listFolderPaths));
		btnRemove.setAction(new RemoveFolderAction(listFolderPaths));
		btnUploadFolders.setAction(new UploadFoldersAction(listFolderPaths));
		setlistFolderPathsData();
		btnAdd.setText("Add");
		btnEditCaption.setText("Edit Caption");
		btnRemove.setText("Remove");
		btnUploadFolders.setText("Upload Folders");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				fileService.saveFolderList(folderPaths);
			}
		});
	}
	
	public void getFolderPaths() {
		ArrayList<String> list = fileService.getFolderList();
		if(list == null) {
			folderPaths = new ArrayList<String>();
		}
		else {
			folderPaths = list;
		}
	}
	
	public String[] convertToStringArray() {
		String[] result = new String[folderPaths.size()];
		int count = 0;
		for(String s: folderPaths) {
			result[count] = s;
			count ++;
		}
		return result;
	}
	public void setlistFolderPathsData() {
		listFolderPaths.setListData(convertToStringArray());
	}
	
	public void addFolderPath(String path) {
		folderPaths.add(path);
		setlistFolderPathsData();
	}
}
